import { createVNode, render } from "vue";
import Loading from "./loading.vue";
let isLoading = false;
const loading = () => {
  let container: any = null;
  const showLoading = (
    isMask = true,
    zindex = 1000,
    options = {
      text: "加载中...",
    }
  ) => {
    if (isLoading) {
      return;
    }
    if (container) {
      hiddenLoading();
    }
    container = document.createElement("div");
    container.className = "my_Loading";
    container.style.Zindex = zindex || 2000;
    const vm = createVNode(Loading, { isMask, text: options.text });

    const app = render(vm, container);
    // document.body.append(container.firstElementChild)
    document.body.append(container);
    isLoading = true;
  };
  const hiddenLoading = () => {
    if (container && isLoading) {
      document.body.removeChild(container);
      container = null;
      isLoading = false;
    }
  };
  return {
    showLoading,
    hiddenLoading,
  };
};

export default loading;
